﻿using Common.Contract.Authentication.Record;
using Common.Contract.Paging;
using Common.Contract.SalePerformance.Record;
using Common.Contract.SalePerformance.Request;
using Common.Contract.SalePerformance.Response;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using SalePerformance.Model.Interface;
using SalePerformance.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalePerformance.Core.ServiceCommand
{
    public class RetrieveKpiMsboRawDailyByDaoServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveKpiMsboRawDailyByDaoRequest, RetrieveKpiMsboRawDailyByDaoResponse>
    {
        #region Properties
        public UserRecord Dao;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public FilterRecord SearchColumns = null;
        #endregion
        public RetrieveKpiMsboRawDailyByDaoServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveKpiMsboRawDailyByDaoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
            SearchColumns = Request.Filter;
        }

        protected override bool OnExecute(RetrieveKpiMsboRawDailyByDaoResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<KpiMsboRawDailyRecord> records = new List<KpiMsboRawDailyRecord>();
                List<TblKpiMsboRaw> listLoans = new List<TblKpiMsboRaw>();
                listLoans = Providers.SalePerformance.RetrieveKpiMsboRawByDao(out totalRecord, Dao, SearchColumns, pageSize, pageIndex);
                if (listLoans != null)
                {
                    records = listLoans.Select(x => new KpiMsboRawDailyRecord
                               {
                                   Dao = x.Dao,
                                   SaleName = x.SaleName,
                                   AvgbalDepositAct = x.AvgbalDepositAct,
                                   AvgbalDepositReal = x.AvgbalDepositReal,
                                   AvgbalDepositTarget = x.AvgbalDepositTarget,
                                   AvgbalLoanAct = x.AvgbalLoanAct,
                                   AvgbalLoanReal = x.AvgbalLoanReal,
                                   AvgbalLoanTarget = x.AvgbalLoanTarget,
                                   AvgbalUplLoanAct = x.AvgbalUplLoanAct,
                                   AvgbalUplLoanReal = x.AvgbalUplLoanReal,
                                   AvgbalUplLoanTarget = x.AvgbalUplLoanTarget,
                                   CreditCardAct = x.CreditCardAct,
                                   CreditCardReal = x.CreditCardReal,
                                   CreditCardTarget = x.CreditCardTarget,
                                   CustomerAct = x.CustomerAct,
                                   CustomerTarget = x.CustomerTarget,
                                   CustomerReal = x.CustomerReal,
                                   NewnplLoanAct = x.NewnplLoanAct,
                                   NewnplLoanReal = x.NewnplLoanReal,
                                   NewnplLoanTarget = x.NewnplLoanTarget
                               }).ToList();
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }
            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
